<?php
require_once ('code/Tasks.php');

if(isset($_GET['action'])) {
    $action = trim($_GET['action']);
    
    if($action == "addtaskupdate") {        
        if((isset($_GET['update']))
        && (isset($_GET['userid']))
        && (isset($_GET['projectid']))
        && (isset($_GET['taskid']))) {
            $updateText = trim($_GET['update']);
            $userid = trim($_GET['userid']);
            $projectid = trim($_GET['projectid']);
            $taskid = trim($_GET['taskid']);

            
            $tasksObj = new Tasks();
            if($updateText != "") {
                $result = $tasksObj->saveTaskUpdates($userid, $projectid, $taskid, $updateText);
            }    
            
            header("Content-type: text/html");
            echo $result;
        }
    } else if($action == "refreshtaskupdate") {
        $result;
        
        if(isset($_GET['taskid'])) {
            $taskid = trim($_GET['taskid']);
            
            $tasksObj = new Tasks();
            if($taskid != "") {
                $result = $tasksObj->getTaskUpdates($taskid);                
            }    
            header("Content-type: application/json");
            echo $result;
        }    
    } else if($action == "saveproject") {
        /*
         * 'userid': userid,
                            'projectid':'0',
                            'projectname': projectname,
                            'projectdesc':projectdesc
         */
        
        if(isset($_GET['userid']) && isset($_GET['projectid']) && isset($_GET['projectname']) && isset($_GET['projectdesc'])) {
            $result;
            
            $userid = trim($_GET['userid']);
            $projectid = trim($_GET['projectid']);
            $projectname = trim($_GET['projectname']);
            $description = trim($_GET['projectdesc']);
        
            $tasksObj = new Tasks();
            if($tasksObj->saveProject($userid, $projectid, $projectname, $description)) {
                $result = '{"result":"success"}';
            } else {
                $result = '{"result":"failure"}';
            }
            
            header("Content-type: application/json");
            echo $result;
        }
    } else if($action == "updatetasksorder") {
        /*
         * 'userid': userid,
                            'projectid':'0',
                            'projectname': projectname,
                            'projectdesc':projectdesc
         */
        
        
        if(isset($_GET['userid']) && isset($_GET['projectid']) && isset($_GET['priorities'])) {
            $result;
            
            $userid = trim($_GET['userid']);
            $projectid = trim($_GET['projectid']);
            $priorities = trim($_GET['priorities']);
        
            $tasksObj = new Tasks();
            if($tasksObj->updateTaskOrder($userid, $projectid, $priorities)) {
                $result = '{"result":"success"}';
            } else {
                $result = '{"result":"failure"}';
            }
            
            header("Content-type: application/json");
            echo $result;
        }
        
        
    }
}

?>
